package com.lzm.seckill.server.controller;

import com.lzm.seckill.model.entity.ItemKill;
import com.lzm.seckill.server.service.impl.ItemService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/**
 * Description:
 *
 * @Author: Liuzemin
 * @Date: 2020/9/14 19:21
 */
@Controller
public class ItemController {
    private final static Logger log = LoggerFactory.getLogger(ItemController.class);

    private final static String prefix = "item";

    @Autowired
    private ItemService itemService;

    /**
     * 获取商品列表
     */
    @RequestMapping(value = {"/","/index",prefix+"/list",prefix+"/index.html"},method = RequestMethod.GET)
    public String list(ModelMap modelMap){
        try {
            List<ItemKill> list = itemService.getKillItems();
            modelMap.put("list", list);

            log.info("获取待秒杀商品列表-数据 {}", list);
        }catch (Exception e){
            log.error("获取待秒杀商品列表-发生异常：",e.fillInStackTrace());
            return "redirect:/base/error";
        }
        return "list";
    }

    /**
     * 获取待秒杀商品的详情
     * @return
     */
    @GetMapping(value = prefix+"/detail/{id}")
    public String detail(@PathVariable Integer id, ModelMap modelMap){
        if (id==null || id<=0){
            return "redirect:/base/error";
        }
        try {
            ItemKill detail=itemService.getKillDetail(id);
            modelMap.put("detail",detail);
        }catch (Exception e){
            log.error("获取待秒杀商品的详情-发生异常：id={}",id,e.fillInStackTrace());
            return "redirect:/base/error";
        }
        return "info";
    }
}
